<?php

session_start();
include('../Fonctions.php');

$infoConvention = array(
    $miseADisposition = trim(htmlspecialchars(strip_tags($_POST['miseADisposition']))),
    $cables = trim(htmlspecialchars(strip_tags($_POST['cables']))),
    $longueurLigne = trim(htmlspecialchars(strip_tags($_POST['longueurLigne']))),
    $largeurTranchee = trim(htmlspecialchars(strip_tags($_POST['largeurTranchee']))),
    $adresseImplantation = trim(htmlspecialchars(strip_tags($_POST['adresseImplantation']))),
    $section = trim(htmlspecialchars(strip_tags($_POST['section']))),
    $numeroSection = trim(htmlspecialchars(strip_tags($_POST['numeroSection']))),
    $nomPoste = trim(htmlspecialchars(strip_tags($_POST['nomPoste']))),
    $gdo = trim(htmlspecialchars(strip_tags($_POST['gdo']))),
    $longueurDP = trim(htmlspecialchars(strip_tags($_POST['longueurDP']))),
    $largeurDP = trim(htmlspecialchars(strip_tags($_POST['largeurDP']))),
    $hauteurDP = trim(htmlspecialchars(strip_tags($_POST['hauteurDP']))),
    $surfaceDP = trim(htmlspecialchars(strip_tags($_POST['surfaceDP']))),
    $puissance = trim(htmlspecialchars(strip_tags($_POST['puissance']))),
    $typePoste = trim(htmlspecialchars(strip_tags($_POST['typePoste']))),
    $naturePoste = trim(htmlspecialchars(strip_tags($_POST['naturePoste']))),
    $identiteProprio = trim(htmlspecialchars(strip_tags($_POST['identiteProprio']))),
    $denominationSociale = trim(htmlspecialchars(strip_tags($_POST['denominationSociale']))),
    $formeJuridique = trim(htmlspecialchars(strip_tags($_POST['formeJuridique']))),
    $nationalite = trim(htmlspecialchars(strip_tags($_POST['nationalite']))),
    $capitalSocial = trim(htmlspecialchars(strip_tags($_POST['capitalSocial']))),
    $dateConstitution = trim(htmlspecialchars(strip_tags($_POST['dateConstitution']))),
    $lieuConstitution = trim(htmlspecialchars(strip_tags($_POST['lieuConstitution']))),
    $numeroRegistre = trim(htmlspecialchars(strip_tags($_POST['numeroRegistre']))),
    $adresseSiegeSocial = trim(htmlspecialchars(strip_tags($_POST['adresseSiegeSocial']))),
    $represantantAssociation = trim(htmlspecialchars(strip_tags($_POST['represantantAssociation']))),
    $qualiteRepresentant = trim(htmlspecialchars(strip_tags($_POST['qualiteRepresentant']))),
    $telephoneDomicile = trim(htmlspecialchars(strip_tags($_POST['telephoneDomicile']))),
    $adresseCorrespondant = trim(htmlspecialchars(strip_tags($_POST['adresseCorrespondant']))),
    $titrePropriete = trim(htmlspecialchars(strip_tags($_POST['titrePropriete']))),
    $nomConjoint = trim(htmlspecialchars(strip_tags($_POST['nomConjoint']))),
    $prenomConjoint = trim(htmlspecialchars(strip_tags($_POST['prenomConjoint']))),
    $nomJeuneFille = trim(htmlspecialchars(strip_tags($_POST['nomJeuneFille']))),
    $regimeMatrimonial = trim(htmlspecialchars(strip_tags($_POST['regimeMatrimonial']))),
    $mairieVille = trim(htmlspecialchars(strip_tags($_POST['mairieVille']))),
    $nomHabilite = trim(htmlspecialchars(strip_tags($_POST['nomHabilite']))),
    $prenomHabilite = trim(htmlspecialchars(strip_tags($_POST['prenomHabilite']))),
    $adresseHabilite = trim(htmlspecialchars(strip_tags($_POST['adresseHabilite']))),
    $nomPromoteur = trim(htmlspecialchars(strip_tags($_POST['nomPromoteur']))),
    $nomSyndicat = trim(htmlspecialchars(strip_tags($_POST['nomSyndicat']))),
    $adresseSyndicat = trim(htmlspecialchars(strip_tags($_POST['adresseSyndicat']))),
    $nomNotaire = trim(htmlspecialchars(strip_tags($_POST['nomNotaire']))),
    $adresseNotaire = trim(htmlspecialchars(strip_tags($_POST['adresseNotaire']))),
    $jeSoussigne = trim(htmlspecialchars(strip_tags($_POST['jeSoussigne']))),
    $adresseERDF = trim(htmlspecialchars(strip_tags($_POST['adresseERDF']))),
    $lieuFIPPOSTE = trim(htmlspecialchars(strip_tags($_POST['lieuFIPPOSTE']))),
    $date = trim(htmlspecialchars(strip_tags($_POST['dateFIPPOSTE']))),
    $pdf = trim(htmlspecialchars(strip_tags($_POST['valider']))));

foreach ($infoConvention as $cle => $valeur) {
    if ($infoConvention[$cle] == '')
        $infoConvention[$cle] = ".....................";
    echo "clé: " . $cle . " valeur: " . $infoConvention[$cle] . "<br />";
}

$insertionFIPPoste->execute(array(
    'type_etude' => "municipalite",
    'id_etude' => $_SESSION['id_etude'],
    'nom_convention' => "convention FIP Poste",
    'miseADisposition' => $miseADisposition,
    'cables' => $cables,
    'longueurLigne' => $longueurLigne,
    'largeurTranchee' => $largeurTranchee,
    'adresseImplantation' => $adresseImplantation,
    'section' => $section,
    'numeroSection' => $numeroSection,
    'nomPoste' => $nomPoste,
    'gdo' => $gdo,
    'longueurDP' => $longueurDP,
    'largeurDP' => $largeurDP,
    'hauteurDP' => $hauteurDP,
    'surfaceDP' => $surfaceDP,
    'puissance' => $puissance,
    'typePoste' => $typePoste,
    'naturePoste' => $naturePoste,
    'identiteProprio' => $infoConvention[16],
    'denominationSociale' => $infoConvention[17],
    'formeJuridique' => $infoConvention[18],
    'nationalite' => $infoConvention[19],
    'capitalSocial' => $infoConvention[20],
    'dateConstitution' => $infoConvention[21],
    'lieuConstitution' => $infoConvention[22],
    'numeroRegistre' => $infoConvention[23],
    'adresseSiegeSocial' => $infoConvention[24],
    'represantantAssociation' => $infoConvention[25],
    'qualiteRepresentant' => $infoConvention[26],
    'telephoneDomicile' => $infoConvention[27],
    'adresseCorrespondant' => $infoConvention[28],
    'titrePropriete' => $infoConvention[29],
    'nomConjoint' => $infoConvention[30],
    'prenomConjoint' => $infoConvention[31],
    'nomJeuneFille' => $infoConvention[32],
    'regimeMatrimonial' => $infoConvention[33],
    'mairieVille' => $infoConvention[34],
    'nomHabilite' => $infoConvention[35],
    'prenomHabilite' => $infoConvention[36],
    'adresseHabilite' => $infoConvention[37],
    'nomPromoteur' => $infoConvention[38],
    'nomSyndicat' => $infoConvention[39],
    'adresseSyndicat' => $infoConvention[40],
    'nomNotaire' => $infoConvention[41],
    'adresseNotaire' => $infoConvention[42],
    'jeSoussigne' => $infoConvention[43],
    'adresseERDF' => $infoConvention[44],
    'lieuFIPPOSTE' => $infoConvention[45],
    'date' => $infoConvention[46]
));


//Recherche de l'id qui vient d'être attribué
$id_convention = $bdd->prepare("SELECT id_convention_fipposte FROM convention_fip_poste WHERE type_etude = :type_etude AND id_etude = :id_etude AND miseADisposition = :miseADisposition AND 
        cables = :cables AND longueurLigne = :longueurLigne AND largeurTranchee = :largeurTranchee AND adresseImplantation = :adresseImplantation AND section = :section AND numeroSection = :numeroSection AND
        nomPoste = :nomPoste AND gdo  = :gdo AND longueurDP  = :longueurDP AND largeurDP  = :largeurDP AND hauteurDP  = :hauteurDP AND surfaceDP  = :surfaceDP AND 
        puissance  = :puissance AND typePoste  = :typePoste AND naturePoste  = :naturePoste AND identiteProprio = :identiteProprio AND denominationSociale = :denominationSociale AND 
        formeJuridique = :formeJuridique AND nationalite = :nationalite AND capitalSocial = :capitalSocial AND dateConstitution = :dateConstitution AND lieuConstitution = :lieuConstitution AND 
        numeroRegistre = :numeroRegistre AND adresseSiegeSocial = :adresseSiegeSocial AND represantantAssociation  = :represantantAssociation AND qualiteRepresentant  = :qualiteRepresentant AND 
        telephoneDomicile  = :telephoneDomicile AND adresseCorrespondant  = :adresseCorrespondant AND titrePropriete  = :titrePropriete AND nomConjoint = :nomConjoint AND prenomConjoint = :prenomConjoint 
        AND nomJeuneFille = :nomJeuneFille AND regimeMatrimonial = :regimeMatrimonial AND mairieVille = :mairieVille AND nomHabilite = :nomHabilite AND prenomHabilite = :prenomHabilite AND 
        adresseHabilite = :adresseHabilite AND nomPromoteur = :nomPromoteur AND nomSyndicat = :nomSyndicat AND adresseSyndicat = :adresseSyndicat AND nomNotaire = :nomNotaire AND adresseNotaire = :adresseNotaire AND 
        jeSoussigne = :jeSoussigne AND adresseERDF = :adresseERDF AND lieuFIPPOSTE = :lieuFIPPOSTE AND date = :date");

$id_convention->execute(array(
    'type_etude' => "municipalite",
    'id_etude' => $_SESSION['id_etude'],
    'miseADisposition' => $miseADisposition,
    'cables' => $cables,
    'longueurLigne' => $longueurLigne,
    'largeurTranchee' => $largeurTranchee,
    'adresseImplantation' => $adresseImplantation,
    'section' => $section,
    'numeroSection' => $numeroSection,
    'nomPoste' => $nomPoste,
    'gdo' => $gdo,
    'longueurDP' => $longueurDP,
    'largeurDP' => $largeurDP,
    'hauteurDP' => $hauteurDP,
    'surfaceDP' => $surfaceDP,
    'puissance' => $puissance,
    'typePoste' => $typePoste,
    'naturePoste' => $naturePoste,
    'identiteProprio' => $infoConvention[16],
    'denominationSociale' => $infoConvention[17],
    'formeJuridique' => $infoConvention[18],
    'nationalite' => $infoConvention[19],
    'capitalSocial' => $infoConvention[20],
    'dateConstitution' => $infoConvention[21],
    'lieuConstitution' => $infoConvention[22],
    'numeroRegistre' => $infoConvention[23],
    'adresseSiegeSocial' => $infoConvention[24],
    'represantantAssociation' => $infoConvention[25],
    'qualiteRepresentant' => $infoConvention[26],
    'telephoneDomicile' => $infoConvention[27],
    'adresseCorrespondant' => $infoConvention[28],
    'titrePropriete' => $infoConvention[29],
    'nomConjoint' => $infoConvention[30],
    'prenomConjoint' => $infoConvention[31],
    'nomJeuneFille' => $infoConvention[32],
    'regimeMatrimonial' => $infoConvention[33],
    'mairieVille' => $infoConvention[34],
    'nomHabilite' => $infoConvention[35],
    'prenomHabilite' => $infoConvention[36],
    'adresseHabilite' => $infoConvention[37],
    'nomPromoteur' => $infoConvention[38],
    'nomSyndicat' => $infoConvention[39],
    'adresseSyndicat' => $infoConvention[40],
    'nomNotaire' => $infoConvention[41],
    'adresseNotaire' => $infoConvention[42],
    'jeSoussigne' => $infoConvention[43],
    'adresseERDF' => $infoConvention[44],
    'lieuFIPPOSTE' => $infoConvention[45],
    'date' => $infoConvention[46]
));

$idConvention = $id_convention->fetchColumn(0);

if ($miseADisposition == "local") {
    $surfacePosteLocal = trim(htmlspecialchars(strip_tags($_POST['surfacePosteLocal'])));
    $montantERDFLocal = trim(htmlspecialchars(strip_tags($_POST['montantERDFLocal'])));

    $updateLocalFIPPoste->execute(array(
        'surfacePosteLocal' => $surfacePosteLocal,
        'montantERDFLocal' => $montantERDFLocal,
        'type_etude' => "municipalite",
        'id_etude' => $_SESSION['id_etude'],
        'id_convention_fipposte' => $idConvention
    ));
} else if ($miseADisposition == "terrain") {
    $surfacePosteTerrain = trim(htmlspecialchars(strip_tags($_POST['surfacePosteTerrain'])));
    $montantERDFTerrain = trim(htmlspecialchars(strip_tags($_POST['montantERDFTerrain'])));

    $updateTerrainFIPPoste->execute(array(
        'surfacePosteTerrain' => $surfacePosteTerrain,
        'montantERDFTerrain' => $montantERDFTerrain,
        'type_etude' => "municipalite",
        'id_etude' => $_SESSION['id_etude'],
        'id_convention_fipposte' => $idConvention
    ));
}

if ($pdf == "PDF/") {
    header('Location: ../conventionFIPPoste.php?typeEtude=municipalite&idEtude=' . $_SESSION['id_etude'] . '&idConvention=' . $idConvention . '&pdf=oui');
}
?>																				